package Util::FaHuo::FaHuo;
use Moose;
use namespace::autoclean;
use JSON;
use Time::Piece;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

#####################################################
#Input: 
#	tuan_id
#Return: 
#	fahuos	
#####################################################
sub fahuo_search {
	my ($self, $tuan_id) = @_;

	return $self->schema->resultset('FaHuo')->search({tuan_id => $tuan_id }, {});

}

#####################################################
#Input: 
#	tuan_id
#	fahuo_time	
#	fahuo_address	
#	fahuo_count	
#####################################################
sub add_to_fahuo {
	my ($self, $tuan_id, $fahuo_time, $fahuo_address, $fahuo_comment, $fahuo_count, $user_id) = @_;

	my $fahuo_time_d = Time::Piece->strptime($fahuo_time, "%m/%e/%Y %H:%M:%S");
	my $fahuo = $self->schema->resultset('FaHuo')->create({
			tuan_id => $tuan_id,
			fahuo_date => $fahuo_time_d->datetime,
			fahuo_address => $fahuo_address,
			fahuo_no => $fahuo_count,
			fahuo_comment => $fahuo_comment,
			created_by => $user_id,
			updated_by => $user_id,
			});
	return $fahuo;
}

sub find_next_fahuo_count {
	my ($self, $tuan_id) = @_;

	my $current_count = $self->schema->resultset('FaHuo')->search({tuan_id => $tuan_id }, {})->count();
	return $current_count + 1;
			
}

__PACKAGE__->meta->make_immutable;

1;
